local addonName, addon = ...
-- Boss ID --
local bossIds = {
    ["炮舰"] = 32098,
    ["怒之煞"] = 32099,
    ["纳拉克"] = 32518,
    ["乌达斯塔"] = 32519
}
-- UnitClass("player"):职业
local myClass = select(2, UnitClass("player"))
local color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[myClass]
-- 创建一个框架元素
-- newFrame = CreateFrame("frameType"[, "name"][, parent][, "template"][, id])
-- frameType:框架类型;Frame/Button;详见 https://wow.gamepedia.com/API_CreateFrame
local xMainFrame = CreateFrame("Button", "xMainFrame", UIParent)
xMainFrame:SetFrameStrata("HIGH")
xMainFrame:SetWidth(30)
xMainFrame:SetHeight(30)
-- 第一个TOPLEFT指的是当前frame的左上角,第二个TOPLEFT指的是UIParent的左上角
-- frame左上角的点,距离UIParent左上角是70,-85
xMainFrame:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 70, -85)
--xMainFrame:SetBackdrop({ bgFile = "Interface\\AddOns\\abcInterface\\xx" })
--xMainFrame:SetBackdropColor(color.r, color.g, color.b)
xMainFrame:SetMovable(true)
xMainFrame:SetClampedToScreen(true)

xMainFrame:RegisterForDrag("LeftButton", "RightButton")

-- 各类事件 https://www.cnblogs.com/xiii/p/12433798.html
-- ADDON_LOADED
-- PLAYER_LOGOUT 游戏结束
-- PLAYER_ENTERING_WORLD
--xMainFrame:RegisterEvent("ADDON_LOADED")
--xMainFrame:RegisterEvent("PLAYER_LOGOUT")
--xMainFrame:RegisterEvent("PLAYER_ENTERING_WORLD")

xMainFrame:SetScript("OnEvent", function(self, event, ...)
    print("事件:" .. event)
    if event == "ADDON_LOADED" then
        -- 展示lua错误 1:开 0:关
        SetCVar("scriptErrors", 1)
        -- ReloadUI()
        -- 隐藏狮鹫
        --MainMenuBarArtFrame.LeftEndCap:Hide();
        --MainMenuBarArtFrame.RightEndCap:Hide();
        if GetCVar("portal") == "CN" then
            ConsoleExec("portal TW")
        end
        -- 开启反和谐
        -- SET overrideArchive 0
        SetCVar("overrideArchive", 0)
        -- 关闭语言过滤器
        -- SET profanityFilter 0
        SetCVar("profanityFilter", 0)

        self:UnregisterEvent("ADDON_LOADED")
    elseif event == "PLAYER_ENTERING_WORLD" then
        -- 查看跨甲跨武器的外观是否被收藏 1 开启 0 关闭
        -- /console missingTransmogSourceInItemTooltips 1
        SetCVar("missingTransmogSourceInItemTooltips", "1")
        -- 超远视距(默认是1.9)
        -- /console cameraDistanceMaxFactor 2.6     -- 旧的不好用了
        -- /console cameraDistanceMaxZoomFactor 2.6
        SetCVar("cameraDistanceMaxZoomFactor", 2.6)

        self:UnregisterEvent("PLAYER_ENTERING_WORLD")
    end
end)

xMainFrame:SetScript("OnDragStart", function(self)
    self:StartMoving()
end)
xMainFrame:SetScript("OnDragStop", function(self)
    self:StopMovingOrSizing()
end)
xMainFrame:SetScript("OnClick", function(self)
    print(addonName .. " OnClick.")

    -- SendChatMessage("msg"[, "chatType"[, languageID[, "target"]]])
    -- 方式可选项为emote(表情),guild(公会),party(小队du),raid(团队),yell(大喊),say(说话，默认),whisper(密语)
    -- SendChatMessage("test YELL", "CHANNEL")
    -- SendChatMessage("test YELL", "YELL")
    -- SendChatMessage("test EMOTE", "EMOTE")
    -- SendChatMessage("test SAY", "SAY")
    -- SendChatMessage("test party", "party")
    -- SendChatMessage("test guild", "guild")
    -- SendChatMessage("test raid", "raid")

    --local name = UnitName("mouseover")
    --SendChatMessage("test whisper", "whisper", nil, name)
    -- UnitName("target"):名字
    --SendChatMessage("%T test ", "whisper", GetDefaultLanguage("target"), UnitName("target"))

    local target = "target";
    if UnitName("target") == nil then
        target = "player";
    end
    SendChatMessage(UnitRace(target) .. UnitLevel(target) .. UnitSex(target) .. UnitClass(target) .. UnitName(target), "YELL")
    --
    -- print("当前副本难度:" .. GetLegacyRaidDifficultyID() .. "/" .. GetRaidDifficultyID())
    --
    -- if GetRaidDifficultyID() == 14 then
    --     SetRaidDifficultyID(15)
    -- elseif GetRaidDifficultyID() == 15 then
    --     SetRaidDifficultyID(16)
    -- elseif GetRaidDifficultyID() == 16 then
    --     SetRaidDifficultyID(14)
    -- end
    --
    --
    checkBoss(bossIds)

    --if GetCVar("missingTransmogSourceInItemTooltips") == "1" then
    --    SetCVar("missingTransmogSourceInItemTooltips", "0")
    --else
    --    SetCVar("missingTransmogSourceInItemTooltips", "1")
    --end
end)
xMainFrame:SetScript("OnDoubleClick", function(self)
    print("双击")
end)

-- 检查炮舰(四风谷),怒之煞(昆莱山),乌达斯塔(巨兽岛),纳拉克(雷神岛),鲁克玛(阿兰卡枫林),是否击杀宏
function checkBoss(bossIds)
    for k, v in pairs(bossIds) do
        local d = IsQuestFlaggedCompleted(v)
        print(format("%s: %s", k, d and "\124cff00ff00已杀\124r" or "\124cffff0000未杀\124r"))
    end
end

-- 检查清醒的梦魇进度
function checkLucidNightmareProgress()
    local N, t = { "DAL", "ULD", "AQ", "DEEP", "GNOMER", "VAL", "MAZE", "MOUNT" }, { 47826, 47837, 47841, 47850, 47852, 47863, 47881, 47885 }
    for k, v in pairs(N) do
        local d = IsQuestFlaggedCompleted(t[k])
        print(v, "=", d and "\124cFF00FF00" or "\124cFFFF0000NOT", "DONE")
    end
end

function autoResurrect()
    -- 释放灵魂
    RepopMe()
    -- 接受复活,使角色复活
    AcceptResurrect()
    -- 虚弱复活
    AcceptXPLoss()
end

--/run BNSetMatureLanguageFilter(true)
--/run local x =GetLegacyRaidDifficultyID()
--SendChatMessage(x)


-- _G被称为公共环境表
-- 定义插件宏命令 /abcInterface or /abc
_G.SLASH_abcInterface1 = "/abcInterface"
_G.SLASH_abcInterface2 = "/abc"

_G.SlashCmdList.abcInterface = function(msg, input)
    print(msg)
    if msg == "0" then
        print("当前版本:" .. GetBuildInfo() .. "    版本字符串:" .. select(4, GetBuildInfo()))
        print("当前版本视野距离:" .. GetCVar("cameraDistanceMaxZoomFactor"))
    elseif msg == "1" then
        checkBoss(bossIds)
    elseif msg == "2" then
        checkLucidNightmareProgress()
    elseif msg == "3" then
        autoResurrect()
    elseif msg == "4" then
        -- 在队伍已经准备就绪时加入地下城
        LFDDungeonReadyDialogEnterDungeonButton:Click();
        -- 点击接受弹出对话框
        LFDRoleCheckPopupAcceptButton:Click()
    end
end

-- 隐藏狮鹫
-- /script MainMenuBarArtFrame.LeftEndCap:Hide();
-- /script MainMenuBarArtFrame.RightEndCap:Hide();

-- 查看跨甲跨武器的外观是否被收藏 1 开启 0 关闭
-- /console missingTransmogSourceInItemTooltips 1

--  local soundFile1 = tostring("Interface\\AddOns\\FreeMountCd\\sound\\1.mp3")
--  PlaySoundFile(soundFile1)

--  if UnitFactionGroup("player") == "Alliance" then
--      SendChatMessage("我是联盟玩家", "EMOTE")
--  else
--      SendChatMessage("我是部落玩家", "EMOTE")
--  end

-- /script SetRaidDifficultyID(14)      -- 团队难度调成PT难度
-- /script SetRaidDifficultyID(15)      -- 团队难度调成H难度
-- /script SetRaidDifficultyID(16)      -- 团队难度调成M难度
-- /script SetLegacyRaidDifficultyID(3) -- 是调10人难度
-- /script SetLegacyRaidDifficultyID(4) -- 是调25人难度

--Log = System.Logger(addonName)
--Log.LogLevel = 1
--Log:SetPrefix(1, System.Widget.FontColor.GRAY .. _Name .. "[Debug]")
--Log:SetPrefix(2, System.Widget.FontColor.HIGHLIGHT .. _Name .. "[Info]")
--Log:SetPrefix(3, System.Widget.FontColor.RED .. _Name .. "[Warn]")